<template>
  <section>
    <div class="box">
      <div class="square" style="--i:0;"></div>
      <div class="square" style="--i:1;"></div>
      <div class="square" style="--i:2;"></div>
      <div class="square" style="--i:3;"></div>
      <div class="square" style="--i:4;"></div>
      <div class="square" style="--i:5;"></div>

      <div class="container">
        <div class="form">
          <h2>账户交易管理</h2>
          <el-form :model="form" :rules="rules" ref="formRef" class="transaction-form">
            <!-- 存款和取款表单 -->
            <div v-if="currentOperation !== 'transfer'" class="form-section">
              <div class="inputBx">
                <span>账户ID</span>
                <div class="input-container">
                  <font-awesome-icon icon="user" />
                  <el-form-item prop="accountId">
                    <el-input 
                      v-model="form.accountId" 
                      placeholder="请输入账户ID"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="inputBx">
                <span>金额</span>
                <div class="input-container">
                  <font-awesome-icon icon="money-bill" />
                  <el-form-item prop="amount">
                    <el-input 
                      v-model="form.amount" 
                      type="number" 
                      placeholder="请输入金额"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>
            
            <!-- 转账表单 -->
            <div v-else class="form-section">
              <div class="inputBx">
                <span>转出账户</span>
                <div class="input-container">
                  <font-awesome-icon icon="user" />
                  <el-form-item prop="fromAccountId">
                    <el-input 
                      v-model="form.fromAccountId" 
                      placeholder="请输入转出账户ID"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="inputBx">
                <span>转入账户</span>
                <div class="input-container">
                  <font-awesome-icon icon="user" />
                  <el-form-item prop="toAccountId">
                    <el-input 
                      v-model="form.toAccountId" 
                      placeholder="请输入转入账户ID"
                    />
                  </el-form-item>
                </div>
              </div>
              <div class="inputBx">
                <span>转账金额</span>
                <div class="input-container">
                  <font-awesome-icon icon="money-bill" />
                  <el-form-item prop="amount">
                    <el-input 
                      v-model="form.amount" 
                      type="number" 
                      placeholder="请输入转账金额"
                    />
                  </el-form-item>
                </div>
              </div>
            </div>

            <div class="button-group">
              <el-button 
                type="primary" 
                @click="handleDeposit"
                :class="['action-button', { 'active-button': currentOperation === 'deposit' }]"
              >
                <font-awesome-icon icon="plus-circle" /> 存款
              </el-button>
              <el-button 
                type="warning" 
                @click="handleWithdraw"
                :class="['action-button', { 'active-button': currentOperation === 'withdraw' }]"
              >
                <font-awesome-icon icon="minus-circle" /> 取款
              </el-button>
              <el-button 
                type="success" 
                @click="handleTransfer"
                :class="['action-button', { 'active-button': currentOperation === 'transfer' }]"
              >
                <font-awesome-icon icon="exchange-alt" /> 转账
              </el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </section>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { deposit, withdraw, transfer } from '@/api/transaction'
import { library } from '@fortawesome/fontawesome-svg-core'
import { 
  faUser, 
  faMoneyBill, 
  faPlusCircle, 
  faMinusCircle, 
  faExchangeAlt 
} from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

library.add(faUser, faMoneyBill, faPlusCircle, faMinusCircle, faExchangeAlt)

const formRef = ref(null)
const currentOperation = ref('')

const form = reactive({
  accountId: '',
  amount: null,
  fromAccountId: '',
  toAccountId: ''
})

const rules = {
  accountId: [
    { required: true, message: '请输入账户ID', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      const num = Number(value)
      if (isNaN(num) || !Number.isInteger(num) || num <= 0) {
        callback(new Error('请输入有效的账户ID（正整数）'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ],
  fromAccountId: [
    { required: true, message: '请输入转出账户ID', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      const num = Number(value)
      if (isNaN(num) || !Number.isInteger(num) || num <= 0) {
        callback(new Error('请输入有效的账户ID（正整数）'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ],
  toAccountId: [
    { required: true, message: '请输入转入账户ID', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      const num = Number(value)
      if (isNaN(num) || !Number.isInteger(num) || num <= 0) {
        callback(new Error('请输入有效的账户ID（正整数）'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ],
  amount: [
    { required: true, message: '请输入金额', trigger: 'blur' },
    { validator: (rule, value, callback) => {
      const num = Number(value)
      if (isNaN(num)) {
        callback(new Error('请输入有效的数字'))
      } else if (num <= 0) {
        callback(new Error('金额必须大于0'))
      } else {
        callback()
      }
    }, trigger: 'blur' }
  ]
}

const handleDeposit = async () => {
  currentOperation.value = 'deposit'
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      const result = await deposit(Number(form.accountId), Number(form.amount))
      //alert(result.data.code)
      if (result.data.code === 0) {
        ElMessage.success(result.data.message)
        
        formRef.value.resetFields()
      } else {
        //alert(result.data.message)
        ElMessage.error(result.data.message)
      }
    }
  })
}

const handleWithdraw = async () => {
  currentOperation.value = 'withdraw'
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      const result = await withdraw(Number(form.accountId), Number(form.amount))
      if (result.data.code === 0) {
        ElMessage.success(result.data.message)
        formRef.value.resetFields()
      } else {
        ElMessage.error(result.data.message)
      }
    }
  })
}

const handleTransfer = async () => {
  currentOperation.value = 'transfer'
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      const result = await transfer(Number(form.fromAccountId), Number(form.toAccountId), Number(form.amount))
      if (result.data.code === 0) {
        ElMessage.success(result.data.message)
        formRef.value.resetFields()
      } else {
        ElMessage.error(result.data.message)
      }
    }
  })
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=El+Messiri:wght@700&display=swap');

* {
  margin: 0;
  padding: 0;
  font-family: 'El Messiri', sans-serif;
}

section {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.box {
  position: relative;
  width: 100%;
  max-width: 440px;
  min-height: 480px;
  margin-bottom: 60px;
}

.box .square {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 15px;
  animation: square 10s linear infinite;
  animation-delay: calc(-1s * var(--i));
}

@keyframes square {
  0%, 100% {
    transform: translateY(-20px);
  }
  50% {
    transform: translateY(20px);
  }
}

.box .square:nth-child(1) {
  width: 80px;
  height: 80px;
  top: 10px;
  right: 10px;
}

.box .square:nth-child(2) {
  width: 60px;
  height: 60px;
  top: 140px;
  left: 0px;
  z-index: 2;
}

.box .square:nth-child(3) {
  width: 50px;
  height: 50px;
  bottom: 70px;
  right: 10px;
  z-index: 2;
}

.box .square:nth-child(4) {
  width: 40px;
  height: 40px;
  bottom: 40px;
  left: 10px;
}

.box .square:nth-child(5) {
  width: 40px;
  height: 40px;
  top: 10px;
  left: 60px;
}

.box .square:nth-child(6) {
  width: 70px;
  height: 70px;
  top: 190px;
  right: 0px;
  z-index: 2;
}

.container {
  position: relative;
  padding: 40px 50px;
  width: 100%;
  max-width: 400px;
  min-height: 380px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  border-radius: 10px;
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.2);
  margin: 0 auto;
}

.container::after {
  content: '';
  position: absolute;
  top: 5px;
  right: 5px;
  bottom: 5px;
  left: 5px;
  border-radius: 5px;
  pointer-events: none;
  background: linear-gradient(
    to bottom,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.1) 2%
  );
}

.form {
  position: relative;
  width: 100%;
  height: 100%;
}

.form h2 {
  color: #fff;
  letter-spacing: 2px;
  margin-bottom: 30px;
  text-align: center;
}

.inputBx {
  position: relative;
  width: 100%;
  margin-bottom: 20px;
}

.input-container {
  position: relative;
  display: flex;
  align-items: center;
}

.input-container svg {
  position: absolute;
  left: 13px;
  width: 16px;
  height: 16px;
  color: #fff;
  z-index: 1;
}

.inputBx :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.2);
  box-shadow: none;
  border-radius: 15px;
  padding-left: 40px;
}

.inputBx :deep(.el-input__inner) {
  height: 40px;
  color: #fff;
  font-size: 16px;
  background: transparent;
}

.inputBx :deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.7);
}

.inputBx span {
  display: block;
  color: #fff;
  font-size: 14px;
  margin-bottom: 5px;
  font-weight: 500;
}

.button-group {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  gap: 15px;
}

.action-button {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  padding: 8px 20px;
  border-radius: 15px;
  font-size: 16px;
  transition: 0.5s;
}

.action-button:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
}

.active-button {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

:deep(.el-form-item__error) {
  color: #ffd04b;
}

:deep(.el-form-item.is-error .el-input__inner) {
  border-color: #ffd04b;
}
</style>